package com.pet.system.util;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import javax.imageio.ImageIO;

import org.apache.struts2.ServletActionContext;

import com.swetake.util.Qrcode;
import com.pet.framework.util.FileOperate;
import com.pet.framework.util.StringUtil;
import com.pet.system.constants.ATTSystem;

public class QrcodeUtil {

	
	public static String createQrcodePath(String type,Integer id){
		try{
			String url = "http://"+ServletActionContext.getRequest().getServerName()+"/support/wap.shtml?type="+type+"&id="+id;
			String imgName="qr"+type+"_"+id+new Date().getTime()+".jpg";
			String imgPath = ATTSystem.WEB_ROOT+"/userfiles/"+imgName;
			FileOutputStream fos = new FileOutputStream(imgPath);
			ImageIO.write(create_image(url), "jpg", fos);
			String resPath = FileOperate.uploadFile(new File(imgPath), "/userfiles/qrcode/",imgName);
			FileOperate.delFileLocal(imgPath);
			return resPath;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public static BufferedImage create_image(String sms_info)throws Exception{
		BufferedImage bi = new BufferedImage(392, 392, BufferedImage.TYPE_BYTE_BINARY);  
		try{
	            Qrcode qrcode =new Qrcode();
	            qrcode.setQrcodeErrorCorrect('M');
	            qrcode.setQrcodeEncodeMode('B');
	            qrcode.setQrcodeVersion(7);
	            String testString = sms_info;
	            byte[] d = testString.getBytes("gbk");
	            Graphics2D g = bi.createGraphics();
	            g.setBackground(Color.WHITE);
	            g.clearRect(0, 0, 392, 392);
	            g.setColor(Color.BLACK);
	            
	            // 限制最大字节数为120
	            if (d.length>0 && d.length <120){
	                boolean[][] s = qrcode.calQrcode(d);
	                for (int i=0;i<s.length;i++){
	                    for (int j=0;j<s.length;j++){
	                        if (s[j][i]) {
	                            g.fillRect(j*8+16,i*8+16,8,8);
	                        }
	                    }
	                }
	            }
	            g.dispose();
	            bi.flush();  
		        } // end try
		        catch (Exception e) {
		            e.printStackTrace();
		        } // end catch
		        return bi;
		 }
	public static BufferedImage create_imagebak(String sms_info)throws Exception{
		BufferedImage bi = new BufferedImage(98, 98, BufferedImage.TYPE_BYTE_BINARY);  
		try{
	            Qrcode qrcode =new Qrcode();
	            qrcode.setQrcodeErrorCorrect('M');
	            qrcode.setQrcodeEncodeMode('B');
	            qrcode.setQrcodeVersion(7);
	            String testString = sms_info;
	            byte[] d = testString.getBytes("gbk");
	            Graphics2D g = bi.createGraphics();
	            g.setBackground(Color.WHITE);
	            g.clearRect(0, 0, 98, 98);
	            g.setColor(Color.BLACK);
	            
	            // 限制最大字节数为120
	            if (d.length>0 && d.length <120){
	                boolean[][] s = qrcode.calQrcode(d);
	                for (int i=0;i<s.length;i++){
	                    for (int j=0;j<s.length;j++){
	                        if (s[j][i]) {
	                            g.fillRect(j*2+3,i*2+3,2,2);
	                        }
	                    }
	                }
	            }
	            g.dispose();
	            bi.flush();  
		        } // end try
		        catch (Exception e) {
		            e.printStackTrace();
		        } // end catch
		        
		        return bi;
		 }
	
	
	public static void main(String[] args){
		QrcodeUtil.createQrcodePath("asdf", 2);
	}
	
}
